/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.scene;

import com.flexengine.component.ProgressBar;
import com.flexengine.engine.FlexEngine;
import com.flexengine.utils.FontManager;
import com.flexengine.logger.Logger;
import com.flexengine.view.GameWindow;
import com.flexengine.view.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public abstract class LoadScene extends GameScene {

    private int load;
    private int dots;
    private Color loadingColor;
    private Font loadingFont;
    private String loadingMsg;
    private String loadingMsgShow;
    private String credits;
    // -- Daemon thread for load resources...
    private Thread tLoad;
    private ProgressBar progressBar;

    public LoadScene(String name) {
        super(name);
        setLoadResourcesOnInit(false); //-- sanity check...
        load = 0;
        dots = 0;
        loadingColor = Color.white;
        loadingMsg = "Loading";
        loadingMsgShow = loadingMsg;
        credits = "";
        loadingFont = FontManager.getFont("Serif", 1, 22);
        x = 110;
        y = getGameWindow().getHeight() - 100;
        // -- load bar widget
        progressBar = new ProgressBar();
        progressBar.setSize(GameWindow.getInstance().getWidth()-150, 32);
        progressBar.setPosition(
                ProgressBar.SCREEN_HORIZONTAL_CENTER_ALIGN, ProgressBar.SCREEN_VERTICAL_BOTTOM_ALIGN, 
                0, -50);
        this.add(progressBar);
        // -- start load resouces...
        tLoad = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // -- load resources...
                    loadResourcesOnInit();
                    // -- end load...
                    endLoading();
                } catch (Exception e) {
                    Logger.logWarning( getNome() + ": " + e);
                }
            }
        }, name + "Thread");
        tLoad.setDaemon(true);
    }

    public LoadScene() {
        this("LoadState");
    }
    
    public Color getLoadingColor() {
        return loadingColor;
    }

    public String getLoadingMsg() {
        return loadingMsg;
    }

    public Font getLoadingFont() {
        return loadingFont;
    }

    public String getCredits() {
        return credits;
    }

    public void setLoadingMsg(String loadingMsg) {
        this.loadingMsg = loadingMsg;
        this.loadingMsgShow = loadingMsg;
    }

    public void setLoadingColor(Color loadingColor) {
        this.loadingColor = loadingColor;
    }

    public void setCredits(String credits) {
        this.credits = credits;
    }

    @Override
    public void init(SceneBundle bundle) {
        super.init(bundle);
        tLoad.start();
    }

    @Override
    public void end(SceneBundle bundle) {
        super.end(bundle);
        tLoad = null;
    }

    @Override
    public void update(Long time, Long totalTime) {
        super.update(time,totalTime);
        load++;
        if (load == FlexEngine.DEFAULT_UPS / 4) {
            load = 0;
            dots++;
            loadingMsgShow += ".";
            if (dots > 3) {
                dots = 0;
                loadingMsgShow = loadingMsg;
            }
        }
    }
    
    private int x, y;

    @Override
    public void render(Graphics g) {
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // -- loading
        super.render(g);
        g.setColor(loadingColor);
        g.setFont(loadingFont);
        g.drawString(loadingMsgShow, x, y);
        // -- credits
        g.setFont(FontManager.getFont("Arial", 0, 14));
        if (credits.length() > 0 ) {
            int xW = (int) (g.getFont().getStringBounds(credits, new FontRenderContext(null, true, false)).getWidth() / 2);
            g.drawString(credits, getGameWindow().getWidth() / 2 - xW, getGameWindow().getHeight() - 10);
        }
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    }

    public ProgressBar getProgressBar() {
        return progressBar;
    }

    /**
     * Call when load resources has finished...
     * You can change the scene here.
     */
    public abstract void endLoading();
    
}